<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *理发师管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$workertitle=$this->setting['workertitle']?:'理发师';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','sid','name','realname','lev_id','qz','years','xprice','yprice','txmoney','commission','headimg','remark','islock','status','createtime');
	$list=pdo_getslice('jy_meifa_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['stitle']=pdo_getcolumn('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['sid']),'title');
			$item['lev_name']=pdo_getcolumn('jy_meifa_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['lev_id']),'lev_name');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>$workertitle.'列表'),
			array('op'=>'edit','title'=>'添加'.$workertitle),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>$workertitle.'英文名','type'=>'string'),
			array('name'=>'realname','title'=>$workertitle.'姓名','type'=>'string'),
			array('name'=>'qz','title'=>$workertitle.'前缀','type'=>'string'),
			array('name'=>'headimg','title'=>$workertitle.'头像','type'=>'image'),
			array('name'=>'stitle','title'=>'所属门店','type'=>'string'),
			array('name'=>'lev_name','title'=>'等级名称','type'=>'string'),
			array('name'=>'yprice','title'=>'原价','type'=>'string'),
			array('name'=>'xprice','title'=>'现价','type'=>'string'),
			array('name'=>'txmoney','title'=>'提现总金额','type'=>'string'),
			array('name'=>'commission','title'=>'可提现佣金总额','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'islock','title'=>'是否锁定','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>$workertitle.'英文名','type'=>'string','width'=>'15'),
			array('name'=>'realname','title'=>$workertitle.'姓名','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'状态','type'=>'select','options'=>'status','width'=>'15'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'关闭'),array('id'=>'1','title'=>'开启'));
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_meifa_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回理发师列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>$workertitle.'英文名','type'=>'string','help'=>'填写后将展示在客户端'),
					array('name'=>'realname','title'=>$workertitle.'姓名','type'=>'string'),
					array('name'=>'sid','title'=>'所属门店','type'=>'select','options'=>'stitle','option_value'=>'id'),
					array('name'=>'headimg','title'=>$workertitle.'头像','type'=>'image'),
					array('name'=>'qz','title'=>$workertitle.'前缀','type'=>'string','help'=>'填写一位大写字母'),
					array('name'=>'mid','title'=>'绑定员工会员id','type'=>'choose','action'=>'get_member'),
					array('name'=>'yprice','title'=>'原价','type'=>'string'),
					array('name'=>'xprice','title'=>'现价','type'=>'string'),
					array('name'=>'lev_id','title'=>'等级名称','type'=>'select','options'=>'lev','option_value'=>'id','option_title'=>'lev_name'),
					array('name'=>'years','title'=>'从事行业年限','type'=>'string'),
				//	array('name'=>'mid','title'=>'绑定理发师会员id','type'=>'string','help'=>'填系统会员ID，<a href="./index.php?plugin=core&action=member.member" target="_blank">点击~快速获取会员ID</a>'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
					array('name'=>'islock','title'=>'是否锁定','type'=>'switch'),
					array('name'=>'remark','title'=>$workertitle.'介绍','type'=>'text'),
				)
			),
		)
	);

	$return['lev']=pdo_getall('jy_meifa_lev',array('uniacid'=>$_SESSION['uniacid']),array('id','lev_name'));
	
	$return['stitle']=pdo_getall('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	//print_r($return['lev']);exit;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['name'])){
		exi('请设置英文名','error');
	}
	if(empty($postdata['qz'])){
		exi('请设置前缀','error');
	}
	if(!$postdata['lev_id']){
		exi('请设置等级','error');
	}
	if(!$postdata['xprice']){
		exi('请设置现价','error');
	}
	$data=array(
		'name'=>trim($postdata['name']),
		'realname'=>trim($postdata['realname']),
		'qz'=>trim($postdata['qz']),
		'sid'=>intval($postdata['sid']),
		'headimg'=>trim($postdata['headimg']),
		'lev_id'=>intval($postdata['lev_id']),
		'years'=>intval($postdata['years']),
		'yprice'=>intval($postdata['yprice']),
		'xprice'=>intval($postdata['xprice']),
		'mid'=>intval($postdata['mid']),
		'commission'=>intval($postdata['commission']),
		'txmoney'=>intval($postdata['txmoney']),
		'remark'=>trim($postdata['remark']),
		'status'=>intval($postdata['status']),
		//'mid'=>intval($postdata['mid']),
		'islock'=>intval($postdata['islock']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_meifa_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_meifa_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_meifa_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的'.$workertitle.'！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_meifa_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isshow'])){
		pdo_update('jy_meifa_worker',array('isshow'=>intval($post['isshow'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['islock'])){
		pdo_update('jy_meifa_worker',array('islock'=>intval($post['islock'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}